package com.nondo.dean.auth.controller.api;

import com.nondo.dean.auth.common.bean.PositionItem;
import com.nondo.dean.auth.domain.Company;
import com.nondo.dean.auth.domain.Position;
import com.nondo.dean.auth.provider.PositionManager;
import com.nondo.dean.auth.service.CompanyService;
import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import com.nondo.dean.common.web.View;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * project：dean-cloud
 * 岗位对外接口
 *
 * @author linfeng @ nondo
 * @date 2018/4/23
 */
@RestController
@RequestMapping("/{v}/position")
public class PositionController {

    @Autowired
    private PositionManager positionManager;
    @Autowired
    private CompanyService companyService;


    /**
     * 请求保存岗位
     *
     * @param item 岗位信息
     * @return 信息提示
     */
    @ApiVersion(1)
    @RequestMapping(
            value = "/save",
            method = RequestMethod.POST
    )
    public View<String> requestSave(@Validated @RequestBody PositionItem item) {

        return positionManager.requestSave(item);
    }

    /**
     * 请求删除岗位
     *
     * @param positionId 岗位ID
     * @return 信息提示
     */
    @ApiVersion(1)
    @RequestMapping(
            value = "/delete/{positionId}",
            method = RequestMethod.POST
    )
    public View<String> requestDelete(@PathVariable Long positionId) {

        return positionManager.requestDelete(positionId);
    }

    /**
     * 请求删除岗位
     *
     * @param positionIds 岗位ID数组
     * @return 信息提示
     */
    @ApiVersion(1)
    @RequestMapping(
            value = "/multi/delete",
            method = RequestMethod.POST
    )
    public View<String> requestMultiDelete(@RequestBody Long[] positionIds) {

        return positionManager.requestMultiDelete(positionIds);
    }

    /**
     * 请求查询岗位
     *
     * @param item 岗位信息
     * @return 信息提示
     */
    @ApiVersion(1)
    @RequestMapping(
            value = "/list",
            method = RequestMethod.POST
    )
    public View<Page<Position>> requestQuery(@RequestBody Query<PositionItem> item) {

        PositionItem positionItem = item.getData();
        if (positionItem != null) {
            if (positionItem.getCompanyId() != null) {
                List<Company> companyList = companyService.findByParentCompanyId(positionItem.getCompanyId());
                List<Long> companyIds = companyList.stream().map(Company::getId).collect(Collectors.toList());
                positionItem.setCompanyIds(companyIds);
                positionItem.setCompanyId(null);
            }
        }
        Query<PositionItem> positionItemQuery = new Query<>();
        positionItemQuery.setPage(item.getPage());
        positionItemQuery.setSize(item.getSize());
        positionItemQuery.setData(positionItem);

        return positionManager.requestQuery(positionItemQuery);
    }


}
